package com.atguigu.eduService.service.impl;

import com.atguigu.eduService.entity.EduTeacher;
import com.atguigu.eduService.mapper.EduTeacherMapper;
import com.atguigu.eduService.service.EduTeacherService;
import com.atguigu.eduService.vo.TeacherQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author Chen Wen bo
 * @since 2021-07-09
 */
@Service
@Transactional
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    @Resource
    private EduTeacherMapper eduTeacherMapper;

    //查询所有讲师
    @Override
    public List<EduTeacher> findAllTeacher() {
        return eduTeacherMapper.selectList(null);
    }

    //逻辑删除讲师
    @Override
    public void deleteTeacherById(String id) {
        eduTeacherMapper.deleteById(id);
    }

    //分页查询讲师
    @Override
    public IPage<EduTeacher> selectTeacherPage(Long current, Long size) {
        Page<EduTeacher> page = new Page<>(current, size);
        return eduTeacherMapper.selectPage(page,null);
    }

    //根据分页条件查询
    @Override
    public IPage<EduTeacher> selectTeacherPageCondition(Long current, Long size, TeacherQueryVo teacherQueryVo) {
        Page<EduTeacher> page = new Page<>(current, size);
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(teacherQueryVo.getName())) {
            wrapper.like("name",teacherQueryVo.getName());
        }
        if (!StringUtils.isEmpty(teacherQueryVo.getLevel())) {
            wrapper.eq("level",teacherQueryVo.getLevel());
        }
        if (!StringUtils.isEmpty(teacherQueryVo.getBegin())) {
            wrapper.ge("gmt_create",teacherQueryVo.getBegin());
        }
        if (!StringUtils.isEmpty(teacherQueryVo.getEnd())) {
            wrapper.ge("gmt_create",teacherQueryVo.getEnd());
        }
        //排序
        wrapper.orderByDesc("gmt_create");
        return eduTeacherMapper.selectPage(page,wrapper);
    }

    //添加讲师
    @Override
    public void addTeacher(EduTeacher eduTeacher) {
        eduTeacherMapper.insert(eduTeacher);
    }

    @Override
    public EduTeacher getTeacherById(String id) {
        return eduTeacherMapper.selectById(id);
    }

    @Override
    public void updateTeacher(EduTeacher eduTeacher) {
        eduTeacherMapper.updateById(eduTeacher);
    }

    //分页查询讲师
    @Override
    public Map<String,Object> getTeacherFrontList(Long page, Long limit) {
        QueryWrapper<EduTeacher> eduTeacherQueryWrapper = new QueryWrapper<>();
        eduTeacherQueryWrapper.orderByAsc("sort");
        Page<EduTeacher> teacherPage = new Page<>(page, limit);
        eduTeacherMapper.selectPage(teacherPage, eduTeacherQueryWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("items", teacherPage.getRecords());
        map.put("current", teacherPage.getCurrent());
        map.put("pages", teacherPage.getPages());
        map.put("size", teacherPage.getSize());
        map.put("total", teacherPage.getTotal());
        map.put("hasNext", teacherPage.hasNext());
        map.put("hasPrevious", teacherPage.hasPrevious());
        return map;
    }
}
